package services

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"mall_frontend_api/biz/dal"
	"mall_frontend_api/biz/handler/base"
	"mall_frontend_api/biz/model/region"
	"mall_frontend_api/biz/pack"
)

type RegionService struct {
	ctx    context.Context
	common *base.Response
	dao    *dal.InitDAO
}

func NewRegionService(ctx context.Context, common *base.Response) *RegionService {
	return &RegionService{ctx: ctx, common: common, dao: dal.NewInitDAO()}
}

func (r *RegionService) GetList(req *region.GetRegionListRequest) ([]*region.Region, error) {
	list, err := r.dao.RegionDAO.GetList(r.ctx, []gen.Condition{dal.Region.Pid.Eq(req.GetPID())}, 0, 0)
	if err != nil {
		hlog.CtxErrorf(r.ctx, "GetRegionList error: %v", err)
		return nil, err
	}
	return pack.RegionList(list), nil
}
